<?php

namespace app\api\controller\ai;

use thinkct\controller\Frontend;
use app\common\model\ai\Group as GroupModel;
use app\common\model\ai\app\Models as AppModel;
use app\common\model\ai\Message as MessageModel;
use app\common\model\ai\Models;
use app\admin\model\ai\model\Key as KeyModel;
use thinkct\facade\ChatAI;
use thinkct\facade\ChatData;
use ba\Date;

class Group extends Frontend
{
    protected array $noNeedLogin = ['index', 'get_group_model'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 获取聊天列表
     */
    public function index(): void
    {
        $title = $this->request->get('title', '');

        $group = GroupModel::where(['user_id'=>$this->auth->id, 'status'=>1]);
        if(!empty($title)) {
            $group = $group->where([['title|name', 'like', '%' . $title . '%']]);
        }
        $group = $group->order('top', 'desc')->order('update_time', 'desc')->select();

        // 遍历数据 增加message_count
        foreach ($group as $key => $item) {
            $message_count = MessageModel::where(['group_id'=>$item['id']])->count();
            $group[$key]['message_count'] = $message_count;
            //$group[$key]['update_time'] = Date::human($item['update_time']);
            $update_time = $item['update_time'];
            // 今天
            if (date('Y-m-d', $update_time) == date('Y-m-d')) {
                $group[$key]['update_time'] = date('H:i', $update_time);
            } else {
                $group[$key]['update_time'] = date('m-d', $update_time);
            }
        }
        $this->success('获取成功', [
            'list' => $group,
            'data' => $group
        ]);
    }

    /**
     * 创建聊天列表
     */
    public function create(): void
    {
        $title = $this->request->post('title', '新的聊天');
        $group = ChatData::createGroup($title, $this->auth->id);
        if($group) {
            $this->success('创建成功', $group);
        } else {
            $this->error(ChatData::getError());
        }
    }

    /**
     * 更新聊天列表
     */
    public function update(): void
    {
        $params = $this->request->post();
        $id = $params['id'];
        // 更新列表
        $chatList = GroupModel::where(['id'=>$id, 'user_id'=> $this->auth->id])->find();
        // 通过模型获取id
        if (!empty($params['model'])) {
            $model = Models::where(['name'=>$params['model']])->find();
            if ($model) {
                $params['model_id'] = $model['id'];
            }
        }

        if ($chatList) {

            $chatList->save($params);
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 删除对话组
     */
    public function delete(): void
    {
        $id = $this->request->post('id', '');

        // 删除列表
        $chatList = GroupModel::where(['id'=>$id, 'user_id'=> $this->auth->id])->delete();
        if ($chatList) {
            // 删除列表下的聊天记录
            MessageModel::where(['group_id'=>$id])->delete();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 获取组模型
     */
    public function get_group_model()
    {
        $group_id = $this->request->get('group_id');
        // 获取组信息
        $group = GroupModel::where(['id'=>$group_id, 'user_id'=>$this->auth->id])
            ->field('id, model_id, prompt, model, restriction, temperature, max_reply_token')
            ->find();
        if (!$group) {
            // 默认模型
            $group['prompt'] = '你是一个智能助手，你的任务是帮助我回答问题。';
            $group['model'] = 'gpt-3.5-turbo';
            $group['restriction'] = 20;
            $group['max_reply_token'] = 2000;
        }
        // 获取可用的模型
        $model_list = ChatAI::getModel();
        $group['model_list'] = $model_list;
        // 返回信息
        $this->success('获取成功', $group);
    }
}